package com.siyuanren.gcocp.service.impl;


import cn.hutool.core.date.DateUtil;
import com.siyuanren.gcocp.common.config.PublicConfig;
import com.siyuanren.gcocp.common.util.FileUtil;
import com.siyuanren.gcocp.dto.wx.WxUserDTO;
import com.siyuanren.gcocp.entity.WxUser;
import com.siyuanren.gcocp.entity.WxUserExample;
import com.siyuanren.gcocp.mapper.MyWxUserMapper;
import com.siyuanren.gcocp.service.WxUserService;
import com.vdurmont.emoji.EmojiParser;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author PVer
 */
@Slf4j
@Service
public class WxUserServiceImpl implements WxUserService {


    @Autowired
    private MyWxUserMapper userMapper;

    @Override
    public WxUser getById(int id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public WxUser getByOpenid(String openid) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("openid", openid);
//        return userDAO.getByParam(paramMap);
        WxUserExample example = new WxUserExample();
        example.or().andOpenidEqualTo(openid);
        List<WxUser> wxUsers = userMapper.selectByExample(example);
        if (wxUsers.isEmpty()) {
            return null;
        } else {
            return wxUsers.get(0);
        }
    }

    @Override
    @Transactional
    public synchronized int insert(WxUser entity) {
        entity.setUserNo(String.valueOf(DateUtil.currentSeconds()));
        return userMapper.insertSelective(entity);
    }

    @Override
    @Transactional
    public WxUser insertWxMpUser(WxMpUser user) {
        WxUser entity = new WxUser();
        wxMpUser2WxUser(user, entity);
        insert(entity);
        return entity;
    }

    @Override
    @Transactional
    public int update(WxUser entity) {
        return userMapper.updateByPrimaryKey(entity);
    }

    @Override
    @Transactional
    public int update(WxUser entity, WxMpUser user) {
        wxMpUser2WxUser(user, entity);
        return userMapper.updateByPrimaryKey(entity);
    }

    private void entity2DTO(WxUser entity, WxUserDTO dto) {
        if (entity == null || dto == null) {
            return;
        }
        BeanUtils.copyProperties(entity, dto);
    }

    private void wxMpUser2WxUser(WxMpUser user, WxUser entity) {
        if (user == null || entity == null) {
            return;
        }
        String openId = user.getOpenId();
        Integer groupId = user.getGroupId();
        String headImgUrl = user.getHeadImgUrl();
        String city = user.getCity();
        String country = user.getCountry();
        String language = user.getLanguage();
        String nickname = user.getNickname();
        String province = user.getProvince();
        Integer sex = user.getSex();
        String sexDesc = user.getSexDesc();
        String remark = user.getRemark();
        Long subscribeTime = user.getSubscribeTime();
        Boolean subscribe = user.getSubscribe();
        entity.setOpenid(openId);
        if (groupId != null) {
            entity.setGroupid(groupId);
        }
        if (StringUtils.isNotBlank(headImgUrl)) {
            entity.setHeadImgurl(headImgUrl);
            String userImg = openId + ".jpg";
            FileUtil.downloadPicture(headImgUrl, PublicConfig.USER_DATA_PATH.concat(openId).concat(".jpg"));
            entity.setUserImg(userImg);
        }
        if (StringUtils.isNotBlank(city)) {
            entity.setCity(city);
        }
        if (StringUtils.isNotBlank(country)) {
            entity.setCountry(country);
        }
        if (StringUtils.isNotBlank(language)) {
            entity.setLanguage(language);
        }
        if (StringUtils.isNotBlank(nickname)) {
            nickname = EmojiParser.removeAllEmojis(nickname);
            entity.setNickname(nickname);
        }
        entity.setAvailable(true);
        if (StringUtils.isBlank(entity.getUserName())) {
            entity.setUserName(nickname);
        }
        if (StringUtils.isNotBlank(province)) {
            entity.setProvince(province);
        }
        if (sex != null) {
            entity.setSex(sex);
        }
        if (StringUtils.isNotBlank(sexDesc)) {
            entity.setSexDesc(sexDesc);
        }
        if (StringUtils.isNotBlank(remark)) {
            entity.setRemark(remark);
        }
        if (subscribeTime != null) {
            entity.setSubscribetime(subscribeTime.intValue());
        }
        if (subscribe!=null) {
            entity.setSubscribe(subscribe);
        }else {
            entity.setSubscribe(false);
        }
    }
}
